<template>
  <div class="user-list">
    <el-dialog title="新增用户" :visible.sync="dialog" width="40%" center>
      <el-form label-position="right" :rules="rules" ref="userForm" label-width="80px" :model="form">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-select class="role-select" v-model="form.role" placeholder="请选择">
            <el-option
              v-for="item in roleList"
              :key="item.code"
              :label="item.name"
              :value="item._id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialog = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>

    <i class="el-icon-edit" title="书写" @click="edit"></i>
    <el-table :data="userList" border style="width: 100%">
      <el-table-column prop="username" label="用户名"></el-table-column>
      <el-table-column prop="nickName" label="姓名"></el-table-column>
      <el-table-column label="角色">
        <template slot-scope="scope">
          {{scope.row.role.name}}
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="200">
        <template slot-scope="scope">
          <el-button type="text" @click="del(scope.row)" size="small" :disabled="scope.row.username === 'root'">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { MD5 } from "crypto-js";
export default {
  name: "User",
  data() {
    return {
      form: {
        usermaname: "",
        password: "",
        nickName: "",
        role: ""
      },
      dialog: false,
      rules: {
        username: [
          { required: true, message: "请输入用户名!", trigger: "blur" },
          {
            min: 4,
            max: 20,
            message: "用户名长度在 4 到 20 个字符!",
            trigger: "blur"
          }
        ],
        password: [
          { required: true, message: "请输入密码!", trigger: "blur" },
          {
            min: 6,
            max: 20,
            message: "密码长度在 6 到 20 个字符!",
            trigger: "blur"
          }
        ],
        nickName: [
          { required: true, message: "请输入昵称！", trigger: "blur" }
        ],
        role: [{ required: true, message: "请选择角色!", trigger: "change" }]
      }
    };
  },
  computed: {
    ...mapGetters(["userList", "roleList"])
  },
  methods: {
    ...mapActions([
      "USER_LIST_ACTION",
      "ROLE_LIST_ACTION",
      "USER_CREATE_ACTION",
      "USER_DELETE_ACTION"
    ]),
    edit() {
      this.dialog = true;
      this.form = {
        username: "",
        password: "",
        nickName: "",
        role: ""
      };
    },
    async save() {
      this.$refs["userForm"].validate(async valid => {
        if (valid) {
          this.form.password = MD5(this.form.password + this.form.username.slice(0, 4), "hex").toString();
          let result = await this.USER_CREATE_ACTION(this.form);
          this.dialog = false;
          if (result.code === 1) {
            this.USER_LIST_ACTION();
          }
        } else {
          return false;
        }
      });
    },
    async del(item) {
      let result = await this.USER_DELETE_ACTION(item._id);
      if (result.code === 1) {
        this.USER_LIST_ACTION();
      }
    }
  },
  async created() {
    this.USER_LIST_ACTION();
    await this.ROLE_LIST_ACTION();
  }
};
</script>
<style lang="scss" scoped>
.el-container {
  width: 100%;
  height: 100%;
}
.el-header,
.el-footer {
  background-color: #b3c0d1;
  color: #333;
  text-align: center;
  height: 45px !important;
  line-height: 45px;
}

.el-aside {
  background-color: #d3dce6;
  color: #333;
  text-align: center;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
  text-align: center;
}
.el-icon-edit {
  margin-bottom: 1rem;
  font-size: 1.2rem;
}
.el-dialog--center .el-dialog__body {
  padding: 25px 25px 5px;
}
.role-select {
  width: 100%;
}
</style>
